package com.longmao.eduorder.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.longmao.commonutils.IDUtils;
import com.longmao.commonutils.ordervo.CourseWebVoOrder;
import com.longmao.commonutils.ordervo.UcenterMemberOrder;
import com.longmao.eduorder.client.CourseClient;
import com.longmao.eduorder.client.UcenterOrderClient;
import com.longmao.eduorder.entity.TOrder;
import com.longmao.eduorder.mapper.OrderMapper;
import com.longmao.eduorder.service.OrderService;
import com.longmao.servicebase.handler.LongMaoException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * <p>
 * 订单 服务实现类
 * </p>
 *
 * @author longmao
 * @since 2022-02-25
 */
@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, TOrder> implements OrderService {

    @Autowired
    private CourseClient courseClient;

    @Autowired
    private UcenterOrderClient ucenterOrderClient;

    @Override
    public String saveOrder(String courseId, String memberId) {
        if(StringUtils.isEmpty(memberId)) {
            throw new LongMaoException(20001, "请先登录!");
        }
        //通过远程调用的方式获取用户信息
        UcenterMemberOrder userInfoOrder = ucenterOrderClient.getUserInfoOrder(memberId);

        //通过远程调用的方式获取课程信息
        CourseWebVoOrder courseInfoOrder = courseClient.getCourseInfoOrder(courseId);

        TOrder order = new TOrder();
        order.setOrderNo(IDUtils.getDateNo());
        order.setCourseId(courseId);
        order.setCourseTitle(courseInfoOrder.getTitle());
        order.setCourseCover(courseInfoOrder.getCover());
        order.setTeacherName(courseInfoOrder.getTeacherName());
        order.setTotalFee(courseInfoOrder.getPrice());
        order.setMemberId(memberId);
        order.setMobile(userInfoOrder.getMobile());
        order.setNickname(userInfoOrder.getNickname());
        order.setStatus(0);     //支付状态   0：未支付，1：已支付
        order.setPayType(1);    //支付类型
        baseMapper.insert(order);

        return order.getOrderNo();
    }
}
